from typing import Any

from tortoise.query_utils import Q
from tortoise.queryset import QuerySet

from .fastapi_admin.filters import Filter, SearchFilter, register_filter
from .fastapi_admin.site import Field
from typing import Optional
from .models import User
from fastapi import Depends
from .fastapi_admin.depends import get_current_user
@register_filter
class TitleLikeFilter(SearchFilter):
    @classmethod
    def get_queryset(cls, queryset: QuerySet, value: Any) -> QuerySet:
        return queryset.filter(title__icontains=value)
    @classmethod
    async def get_field(cls) -> Field:
        return Field(label="TitleLike", type="text")
    @classmethod
    def get_name(cls) -> str:
        return "filter"

@register_filter
class NameLikeFilter(SearchFilter):
    @classmethod
    def get_queryset(cls, queryset: QuerySet, value: Any) -> QuerySet:
        return queryset.filter(name__icontains=value)
    @classmethod
    async def get_field(cls) -> Field:
        return Field(label="NameLike", type="text")
    @classmethod
    def get_name(cls) -> str:
        return "filter"

class ArticleFilter(Filter):
    @classmethod
    def get_queryset(cls,queryset: QuerySet,user:Optional[User]=None) -> QuerySet:
        if user and not user.is_superuser:
            qs = queryset.filter(author_id=user.pk)
        else:
            qs = queryset.all()
        return qs

